package com.littcore.saap.core.module.portal;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.littcore.common.CoreConstants;
import com.littcore.saap.core.common.SaapConstants;
import com.littcore.util.ResourceUtils;
import com.littcore.util.StringUtils;
import com.thoughtworks.xstream.XStream;


/**
 * Porlet管理器.
 * 
 * <pre><b>描述：</b>
 *    
 * </pre>
 * 
 * <pre><b>修改记录：</b>
 *    
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2015年10月21日
 * @version 1.0
 */
public class PortletMetaManager {

  public static final Logger logger = LoggerFactory.getLogger(PortletMetaManager.class);
  
  public static String BASE_PATH = "portal"; 
  
  private LinkedHashMap<String, PortletMeta> portalMetaMap = new LinkedHashMap<String, PortletMeta>();
  
  public PortletMetaManager()
  {
    init();
  }
  
  public void init()
  {
    logger.info("Init Portal module...");
    //读取系统内置的
    {
      try {
        File dir = ResourceUtils.getFile("classpath:module/portal");
        loadConfig(dir);
      } catch (FileNotFoundException e) {
        logger.error("Load portal config files from classpath failed.", e);
      }
    }
    
    //读取生产环境中保存的，相同的将覆盖系统内置的
    {
      File dir = new File(SaapConstants.HOME_PATH, BASE_PATH);
      loadConfig(dir);
    }     
  }

  private void loadConfig(File dir) 
  {
    if(dir.exists() && dir.isDirectory())
    {
      File[] files = dir.listFiles();
      for (File file : files) {       
        if(!StringUtils.endsWithIgnoreCase(file.getName(), ".xml"))
          continue;
        logger.debug("load portlet config file:{}", new Object[]{file.getName()});
        String name = StringUtils.substringBefore(file.getName(), "-portlet");
        
        XStream xstream = new XStream();
        xstream.alias("portlet-meta", PortletMeta.class);
        xstream.alias("param", PortletMetaParam.class);
        xstream.aliasAttribute(PortletMetaParam.class, "name", "name");
        xstream.aliasAttribute(PortletMetaParam.class, "dataType", "data-type");
        xstream.aliasAttribute(PortletMetaParam.class, "value", "default");
        PortletMeta portletMeta = (PortletMeta)xstream.fromXML(file);
        this.portalMetaMap.put(name, portletMeta);
      }
    }
  } 
  
  public void reload() {
    this.init();
  } 
  
  public PortletMeta getPortletMeta(String uid)
  {
    if(CoreConstants.IS_DEBUG)
      this.init();
    return portalMetaMap.get(uid);
  }
  
  public List<PortletMeta> getAllPortletMeta()
  {   
    if(CoreConstants.IS_DEBUG)
      this.init();
    return new LinkedList<PortletMeta>(this.portalMetaMap.values());
  }
  
  
  public static void main(String[] args) throws Exception {
    PortletMeta meta = PortletMetaManager.getInstance().getPortletMeta("iframe");
    System.out.println(meta.getName());
    System.out.println(meta.getParams().size());
    PortletMetaParam param = meta.getParams().get(1);
    System.out.println(param.getName());
    System.out.println(param.getDataType());
    System.out.println(param.getValue());
  }
  
  private static class SingletonClassInstance { 
      private static final PortletMetaManager instance = new PortletMetaManager(); 
  } 

  /**
   * Gets the single instance of QuickViewManager.
   *
   * @return single instance of QuickViewManager
   */
  public static PortletMetaManager getInstance() { 
      return SingletonClassInstance.instance; 
  }

}
